package org.ucn.dmaa0914.temadesign.group2.modelLayer;

import org.ucn.dmaa0914.temadesign.group2.tuiLayer.Application;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

/**
 * The DVD class represents a DVD, which can have more copies. The DVD class
 * also acts as a container for all it's copies.
 * 
 * @author Kim Johannsen
 * @version 05-11-2014
 */
public class Dvd
{
    private static int idCounter = 1;
    
    private int id;
    private int barcode;
    private String title;
    private String director;
    private String genre;
    private Date publicationDate;
    private HashSet<Copy> copySet;
    
    /**
     * Constructor
     */
    public Dvd(int barcode, String title, String director, String genre, Date publicationDate)
    {
        this.id = idCounter++;
        this.barcode = barcode;
        this.title = title;
        this.director = director;
        this.genre = genre;
        this.publicationDate = publicationDate;
        
        this.copySet = new HashSet<Copy>();
    }
    
    public String makeDescription()
    {
        return "ID: " + String.valueOf(getId()) + "\n" +
               "Title: " + getTitle() + "\n" +
               "Barcode: " + String.valueOf(getBarcode()) + "\n" +
               "Director: " + getDirector() + "\n" +
               "Genre: " + getGenre() + "\n" +
               "Publication Date: " + Application.instance.dateFormat.format(getPublicationDate()) + "\n" +
               "Number of Copies: " + String.valueOf(copySet.size());
    }
    
    public String toString()
    {
        return title + " (" + String.valueOf(barcode) + ")";
    }
    
    public void setId(int id)
    {
        this.id = id;
    }
    
    public void setBarcode(int barcode)
    {
        this.barcode = barcode;
    }
    
    public void setTitle(String title)
    {
        this.title = title;
    }
    
    public void setDirector(String director)
    {
        this.director = director;
    }
    
    public void setGenre(String genre)
    {
        this.genre = genre;
    }
    
    public void setPublicationDate(Date publicationDate)
    {
        this.publicationDate = publicationDate;
    }
    
    public int getId()
    {
        return id;
    }
    
    public int getBarcode()
    {
        return barcode;
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public String getDirector()
    {
        return director;
    }
    
    public String getGenre()
    {
        return genre;
    }
    
    public Date getPublicationDate()
    {
        return publicationDate;
    }
    
    /*
     * Adds a copy to the DVD, if it doesnt already exist
     * in the database.
     * 
     * @param copy The copy to add
     * @return True if the copy was added. False if the copy was already added.
     */
    public boolean addCopy(Copy copy)
    {
        return copySet.add(copy);
    }
    
    /**
     * Checks whether this DVD has a copy.
     * 
     * @param copy The copy to check for
     * @return True if this DVD has this copy. False if not.
     */
    public boolean containsCopy(Copy copy)
    {
        return copySet.contains(copy);
    }
    
    /**
     * Looks up a copy by it's id.
     * 
     * @param id The id of the copy to find.
     * @return The copy with the specified id, or null if the copy is not found.
     */
    public Copy findCopy(int id)
    {
        Iterator<Copy> i = copySet.iterator();
        
        while( i.hasNext() )
        {
            Copy copy = i.next();
            
            if( copy.getId() == id )
            {
                return copy;
            }
        }
        
        return null;
    }
    
    /**
     * Looks up a copy by it's serial number.
     * 
     * @param copyhone The phone number of the copy to look for
     * @return The copy with the specified phone number, or null if the copy is not found.
     */
    public Copy getCopy(String serialNumber)
    {
        Iterator<Copy> i = copySet.iterator();
        
        while( i.hasNext() )
        {
            Copy copy = i.next();
            
            if( copy.getSerialNumber().equals(serialNumber) )
            {
                return copy;
            }
        }
        
        return null;
    }
    
    /**
     * Deletes a copy from the DVD if present
     * 
     * @param copy The copy to delete
     */
    public void deleteCopy(Copy copy)
    {
        copySet.remove(copy);
    }
    
    /**
     * Retrieves all copies of this DVD
     */
    public HashSet<Copy> getAllCopies()
    {
        return copySet;
    }
    
}
